<?php
namespace app\admin\service;
use think\Db;
class  Other{

    //首页数据
    function indexCount(){
        $admin = session('Admin_user_lvtuan');
        //总订单
        $order = Db::name('order');
        $where['o_state']    = array('in','2,3');
        $data['order_sum']   = $order->where($where)->sum('o_money');
        $data['order_count'] = $order->where($where)->count();
        //用户量
        $data['user_count']  = Db::name('user')->count();
        //处理数据
        $where_d['admin_id'] = $admin['admin_id'];
        $where_d['cp_state'] = 1;
        $data['dispose_count'] = Db::name('consult_push')->where($where_d)->count();
        return $data;
    }

    //当日数据
    function indexList(){
        $price               = date('Y-m-d');
        $where['DATE_FORMAT(o_addtime,"%Y-%m-%d")'] = array('eq',$price);
        $where['o_state']    = array('in','2,3');
        $data['list']        = Db::name('order')->where($where)->field('order_id,o_no,o_type_id,o_money')->order('o_addtime')->limit(5)->select();
        $field    = "a.ct_title,a.consult_id,a.ct_addtime,b.ct_name,c.u_phone,c.u_nickname";
        $join     = [
            ['lt_consult_type b','a.consult_type_id=b.consult_type_id','LEFT'],
            ['lt_user c','a.u_uid=c.u_uid','LEFT'],
        ];
        $where_a['a.ct_state'] = 1;
        $where_a['DATE_FORMAT(a.ct_addtime,"%Y-%m-%d")'] = array('eq',$price);
        $data['consult_list'] = Db::name('consult')->alias('a')->join($join)->where($where_a)->field($field)->order('a.ct_addtime desc')->limit(5)->select();
        return $data;
    }

    //新增用户
    function userCountApi(){
        $betime = Date ( 'Y-m');
        //以月为单位，
        $where[]="DATE_FORMAT(a.u_addtime,'%Y-%m')='$betime'";
        $w1 = ' WHERE '.@implode(' AND ',$where);
        $sql="select DATE_FORMAT(a.u_addtime,'%d') as  grouping, count(user_id) as count from lt_user as a $w1 group by grouping";
        $dataArr = Db::query($sql);
        $data     = $this->fateArr($betime,$dataArr);
        if(!empty($data)){
            $response = messageInfo('11000','查询成功',$data);
        }else{
            $response = messageInfo('10008','查询为空');
        }
        return $response;
    }


    //转换成日
    function fateArr($betime,$dataArr,$state=2){
        $timeDae=strtotime($betime);
        $fate=date('t',$timeDae);
        for($i=1;$i<=$fate;$i++){
            $arr[$i]=$i;
        }
        foreach ($arr as $k1=>$v1){
            $data[$k1]['chars'] =(string)$v1.'日';
            $data[$k1]['count'] = '0';
            if($state == 1) $data[$k1]['money'] = '0';
            foreach ($dataArr as $k=>$v){
                if($v['grouping'] == $v1){
                    $data[$k1]['count']=$v['count'];
                    if($state == 1)   $data[$k1]['money'] = $v['money'];
                }
            }
        }
        $coun['chars']=array_column($data,'chars');
        $coun['count']=array_column($data,'count');
        if($state == 1) $coun['money'] = array_column($data,'money');
        return $coun;
    }

    //转换成中文月份
    function dispose($i){
        switch($i){
            case '1':
                $arr='一月';
                break;
            case '2':
                $arr='二月';
                break;
            case '3':
                $arr='三月';
                break;
            case '4':
                $arr='四月';
                break;
            case '5':
                $arr='五月';
                break;
            case '6':
                $arr='六月';
                break;
            case '7':
                $arr='七月';
                break;
            case '8':
                $arr='八月';
                break;
            case '9':
                $arr='九月';
                break;
            case '10':
                $arr='十月';
                break;
            case '11':
                $arr='十一月';
                break;
            case '12':
                $arr='十二月';
                break;
        }
        return $arr;
    }

    //月份计算
    function ageData($betime,$dataArr,$state=2){

        $arr = array();
        for ($i=1;$i<=12;$i++){
            $arr[$i]['chars'] = $this->dispose($i);
            $arr[$i]['count'] = '0';
            if($state == 1) $arr[$i]['money'] = '0';
            $arr[$i]['int']   = '';
            if($i <=9){
                $arr[$i]['int']=$betime.'-0'.$i;
            }else{
                $arr[$i]['int']=$betime.'-'.$i;
            }
        }

        foreach ($arr as $k1=>$v1){
            $data[$k1]['chars'] = $v1['chars'];
            $data[$k1]['count'] = $v1['count'];
            if($state == 1)   $data[$k1]['money'] = $v1['money'];
            foreach($dataArr as $k=>$v){
                if(strtotime($v1['int']) == strtotime($v['grouping'])){
                    $data[$k1]['count'] = $v['count'];
                    if($state == 1)   $data[$k1]['money'] = $v['money'];
                }
            }
        }

        $coun['chars'] = array_column($data,'chars');
        $coun['count'] = array_column($data,'count');
        if($state == 1) $coun['money'] = array_column($data,'money');
        return $coun;
    }

    //新增用户
    function orderChartApi($age,$luna,$state){
        if($state == 1){
            if(!empty($age)){
                $betime  = $age;
            }else{
                $betime  = Date('Y');
            }
            $y = '%Y';
            $y_o = '%Y-%m';
        }else{
            if (! empty ( $luna ) && ! empty ( $age ) || ! empty ( $luna ) || ! empty ( $age )) {
                if (! empty ( $luna ) && empty ( $age )) {
                    $betime = Date ( 'Y' ) . '-0' . $luna;
                } else if (empty ( $luna ) && ! empty ( $age )) {
                    $betime = $age . '-' . Date ( 'm' );
                } else {
                    if($luna < 10){
                        $betime = $age. '-0' . $luna;
                    }else{
                        $betime = $age. '-' .$luna;
                    }
                }
            } else {
                $betime = Date ( 'Y-m' );
            }
            $y = '%Y-%m';
            $y_o = '%d';
        }
        //以月为单位，
        $where[]    = "DATE_FORMAT(a.o_addtime,'$y')='$betime'".' AND '.'a.o_state in(2,3)';
        $w1         = ' WHERE '.@implode(' AND ',$where);
        $sql        = "select DATE_FORMAT(a.o_addtime,'$y_o') as  grouping, count(order_id) as count,sum(o_money) as money from lt_order as a $w1 group by grouping";
        $dataArr    = Db::query($sql);
        if($state == 1){
            $data       = $this->ageData($betime,$dataArr,1);
        }else{
            $data       = $this->fateArr($betime,$dataArr,1);
        }
        if(!empty($data)){
            $response = messageInfo('11000','查询成功',$data);
        }else{
            $response = messageInfo('10008','查询为空');
        }
        return $response;
    }


    //用户统计
    function userChartApi($age,$luna,$state){
        if($state == 1){
            if(!empty($age)){
                $betime  = $age;
            }else{
                $betime  = Date('Y');
            }
            $y = '%Y';
            $y_o = '%Y-%m';
        }else{
            if (! empty ( $luna ) && ! empty ( $age ) || ! empty ( $luna ) || ! empty ( $age )) {
                if (! empty ( $luna ) && empty ( $age )) {
                    $betime = Date ( 'Y' ) . '-0' . $luna;
                } else if (empty ( $luna ) && ! empty ( $age )) {
                    $betime = $age . '-' . Date ( 'm' );
                } else {
                    if($luna < 10){
                        $betime = $age. '-0' . $luna;
                    }else{
                        $betime = $age. '-' .$luna;
                    }
                }
            } else {
                $betime = Date ( 'Y-m' );
            }
            $y = '%Y-%m';
            $y_o = '%d';
        }
        //以月为单位，
        $where[]    = "DATE_FORMAT(a.u_addtime,'$y')='$betime'";
        $w1         = ' WHERE '.@implode(' AND ',$where);
        $sql        = "select DATE_FORMAT(a.u_addtime,'$y_o') as  grouping, count(user_id) as count from lt_user as a $w1 group by grouping";
        $dataArr    = Db::query($sql);

        if($state == 1){
            $data       = $this->ageData($betime,$dataArr);
        }else{
            $data       = $this->fateArr($betime,$dataArr);
        }
        if(!empty($data)){
            $response = messageInfo('11000','查询成功',$data);
        }else{
            $response = messageInfo('10008','查询为空');
        }
        return $response;
    }




    //用户统计
    function consulChartApi($age,$luna,$state){
        if($state == 1){
            if(!empty($age)){
                $betime  = $age;
            }else{
                $betime  = Date('Y');
            }
            $y = '%Y';
            $y_o = '%Y-%m';
        }else{
            if (! empty ( $luna ) && ! empty ( $age ) || ! empty ( $luna ) || ! empty ( $age )) {
                if (! empty ( $luna ) && empty ( $age )) {
                    $betime = Date ( 'Y' ) . '-0' . $luna;
                } else if (empty ( $luna ) && ! empty ( $age )) {
                    $betime = $age . '-' . Date ( 'm' );
                } else {
                    if($luna < 10){
                        $betime = $age. '-0' . $luna;
                    }else{
                        $betime = $age. '-' .$luna;
                    }
                }
            } else {
                $betime = Date ( 'Y-m' );
            }
            $y = '%Y-%m';
            $y_o = '%d';
        }
        //以月为单位，
        $where[]    = "DATE_FORMAT(a.ct_addtime,'$y')='$betime'";
        $w1         = ' WHERE '.@implode(' AND ',$where);
        $sql        = "select DATE_FORMAT(a.ct_addtime,'$y_o') as  grouping, count(consult_id) as count from lt_consult as a $w1 group by grouping";
        $dataArr    = Db::query($sql);

        if($state == 1){
            $data       = $this->ageData($betime,$dataArr);
        }else{
            $data       = $this->fateArr($betime,$dataArr);
        }
        if(!empty($data)){
            $response = messageInfo('11000','查询成功',$data);
        }else{
            $response = messageInfo('10008','查询为空');
        }
        return $response;
    }

    //订单统计
    function orderCount(){
        $order = Db::name('order');
        $where['DATE_FORMAT(o_addtime,\'%Y\')'] = date('Y');
        $where['o_state'] = array('in','2,3');
        $count_n = $order->where($where)->field('count(order_id) as count,sum(o_money) as money')->find();
        $where_a['DATE_FORMAT(o_addtime,\'%Y-%m\')'] = date('Y-m');
        $where_a['o_state'] = array('in','2,3');
        $count_y = $order->where($where_a)->field('count(order_id) as count,sum(o_money) as money')->find();
        $data['count_n'] = $count_n;
        $data['count_y'] = $count_y;
        return $data;
    }

    //用户量统计
    function userCount(){
        $user = Db::name('user');
        $data['count']   = $user->count();
        $where['DATE_FORMAT(u_addtime,\'%Y\')'] = date('Y');
        $data['count_n'] = $user->where($where)->count();
        $where_a['DATE_FORMAT(u_addtime,\'%Y-%m\')'] = date('Y-m');
        $data['count_y'] = $user->where($where_a)->count();
        return $data;
    }

    //用户量统计
    function consultCount(){
        $consult = Db::name('consult');
        $data['count']   = $consult->count();
        $where['DATE_FORMAT(ct_addtime,\'%Y\')'] = date('Y');
        $data['count_n'] = $consult->where($where)->count();
        $where_a['DATE_FORMAT(ct_addtime,\'%Y-%m\')'] = date('Y-m');
        $data['count_y'] = $consult->where($where_a)->count();
        return $data;
    }



}
